#include "Matrix.h"
#include <math.h>

Matrix::Matrix(void) {
	_11 = _12 = _13 = _14 = 0.0;
	_21 = _22 = _23 = _24 = 0.0;
	_31 = _32 = _33 = _34 = 0.0;
	_41 = _42 = _43 = _44 = 0.0;
}

void Matrix::setIdentityMatrix() {
	_12 = _13 = _14 = 0.0;
	_21 = _23 = _24 = 0.0;
	_31 = _32 = _34 = 0.0;
	_41 = _42 = _43 = 0.0;
	_11 = _22 = _33 = _44 = 1.0;
}

void Matrix::setTranslationMatrix(Vector3D const& d) {
	setIdentityMatrix();
	_14 = d.x;
	_24 = d.y;
	_34 = d.z;
}

Matrix Matrix::operator * (const Matrix& m) const {
	Matrix r;
	r._11 = _11 * m._11 + _12 * m._21 + _13 * m._31 + _14 * m._41;
	r._12 = _11 * m._12 + _12 * m._22 + _13 * m._32 + _14 * m._42;
	r._13 = _11 * m._13 + _12 * m._23 + _13 * m._33 + _14 * m._43;
	r._14 = _11 * m._14 + _12 * m._24 + _13 * m._34 + _14 * m._44;
	
	r._21 = _21 * m._11 + _22 * m._21 + _23 * m._31 + _24 * m._41;
	r._22 = _21 * m._12 + _22 * m._22 + _23 * m._32 + _24 * m._42;
	r._23 = _21 * m._13 + _22 * m._23 + _23 * m._33 + _24 * m._43;
	r._24 = _21 * m._14 + _22 * m._24 + _23 * m._34 + _24 * m._44;

	r._31 = _31 * m._11 + _32 * m._21 + _33 * m._31 + _34 * m._41;
	r._32 = _31 * m._12 + _32 * m._22 + _33 * m._32 + _34 * m._42;
	r._33 = _31 * m._13 + _32 * m._23 + _33 * m._33 + _34 * m._43;
	r._34 = _31 * m._14 + _32 * m._24 + _33 * m._34 + _34 * m._44;

	r._41 = _41 * m._11 + _42 * m._21 + _43 * m._31 + _44 * m._41;
	r._42 = _41 * m._12 + _42 * m._22 + _43 * m._32 + _44 * m._42;
	r._43 = _41 * m._13 + _42 * m._23 + _43 * m._33 + _44 * m._43;
	r._44 = _41 * m._14 + _42 * m._24 + _43 * m._34 + _44 * m._44;
	
	return r;
}

Vector3D Matrix::operator * (const Vector3D& v) const {
	Vector3D r;
	r.x = _11 * v.x + _12 * v.y + _13 * v.z + _14;
	r.y = _21 * v.x + _22 * v.y + _23 * v.z + _24;
	r.z = _31 * v.x + _32 * v.y + _33 * v.z + _34;
	return r;
}

Vector4D Matrix::operator * (const Vector4D& v) const {
	Vector4D r;
	r.x = _11 * v.x + _12 * v.y + _13 * v.z + _14;
	r.y = _21 * v.x + _22 * v.y + _23 * v.z + _24;
	r.z = _31 * v.x + _32 * v.y + _33 * v.z + _34;
	r.w = _41 * v.x + _42 * v.y + _43 * v.z + _44;
	return r;
}

Matrix Matrix::invert() {
	Matrix invertetMatrix;
	
	/* NB. OpenGL Matrices are COLUMN major. */
	#define SWAP_ROWS(a, b) { float *_tmp = a; (a)=(b); (b)=_tmp; }

	float wtmp[4][8];
	float m0, m1, m2, m3, s;
	float *r0, *r1, *r2, *r3;
	
	r0 = wtmp[0], r1 = wtmp[1], r2 = wtmp[2], r3 = wtmp[3];
	
	r0[0] = _11, r0[1] = _12,
	r0[2] = _13, r0[3] = _14,
	r0[4] = 1.f, r0[5] = r0[6] = r0[7] = 0.f,
	r1[0] = _21, r1[1] = _22,
	r1[2] = _23, r1[3] = _24,
	r1[5] = 1.f, r1[4] = r1[6] = r1[7] = 0.f,
	r2[0] = _31, r2[1] = _32,
	r2[2] = _33, r2[3] = _34,
	r2[6] = 1.f, r2[4] = r2[5] = r2[7] = 0.f,
	r3[0] = _41, r3[1] = _42,
	r3[2] = _43, r3[3] = _44,
	r3[7] = 1.f, r3[4] = r3[5] = r3[6] = 0.f;
	
	/* choose pivot - or die */
	if (fabsf(r3[0]) > fabsf(r2[0]))
		SWAP_ROWS(r3, r2);
	if (fabsf(r2[0]) > fabsf(r1[0]))
		SWAP_ROWS(r2, r1);
	if (fabsf(r1[0]) > fabsf(r0[0]))
		SWAP_ROWS(r1, r0);
	if (0.f == r0[0])
		return invertetMatrix;
	
	/* eliminate first variable     */
	m1 = r1[0] / r0[0];
	m2 = r2[0] / r0[0];
	m3 = r3[0] / r0[0];
	s = r0[1];
	r1[1] -= m1 * s;
	r2[1] -= m2 * s;
	r3[1] -= m3 * s;
	s = r0[2];
	r1[2] -= m1 * s;
	r2[2] -= m2 * s;
	r3[2] -= m3 * s;
	s = r0[3];
	r1[3] -= m1 * s;
	r2[3] -= m2 * s;
	r3[3] -= m3 * s;
	s = r0[4];
	if (s != 0.f) {
		r1[4] -= m1 * s;
		r2[4] -= m2 * s;
		r3[4] -= m3 * s;
	}
	s = r0[5];
	if (s != 0.f) {
		r1[5] -= m1 * s;
		r2[5] -= m2 * s;
		r3[5] -= m3 * s;
	}
	s = r0[6];
	if (s != 0.f) {
		r1[6] -= m1 * s;
		r2[6] -= m2 * s;
		r3[6] -= m3 * s;
	}
	s = r0[7];
	if (s != 0.f) {
		r1[7] -= m1 * s;
		r2[7] -= m2 * s;
		r3[7] -= m3 * s;
	}
	
	/* choose pivot - or die */
	if (fabsf(r3[1]) > fabsf(r2[1]))
		SWAP_ROWS(r3, r2);
	if (fabsf(r2[1]) > fabsf(r1[1]))
		SWAP_ROWS(r2, r1);
	if (0.f == r1[1])
		return invertetMatrix;
	
	/* eliminate second variable */
	m2 = r2[1] / r1[1];
	m3 = r3[1] / r1[1];
	r2[2] -= m2 * r1[2];
	r3[2] -= m3 * r1[2];
	r2[3] -= m2 * r1[3];
	r3[3] -= m3 * r1[3];
	s = r1[4];
	if (0.f != s) {
		r2[4] -= m2 * s;
		r3[4] -= m3 * s;
	}
	s = r1[5];
	if (0.f != s) {
		r2[5] -= m2 * s;
		r3[5] -= m3 * s;
	}
	s = r1[6];
	if (0.f != s) {
		r2[6] -= m2 * s;
		r3[6] -= m3 * s;
	}
	s = r1[7];
	if (0.f != s) {
		r2[7] -= m2 * s;
		r3[7] -= m3 * s;
	}
	
	/* choose pivot - or die */
	if (fabs(r3[2]) > fabs(r2[2]))
		SWAP_ROWS(r3, r2);
	if (0.f == r2[2])
		return invertetMatrix;
	
	/* eliminate third variable */
	m3 = r3[2] / r2[2];
	r3[3] -= m3 * r2[3], r3[4] -= m3 * r2[4],
	r3[5] -= m3 * r2[5], r3[6] -= m3 * r2[6], r3[7] -= m3 * r2[7];
	
	/* last check */
	if (0.f == r3[3])
		return invertetMatrix;
	
	s = 1.f / r3[3];		/* now back substitute row 3 */
	r3[4] *= s;
	r3[5] *= s;
	r3[6] *= s;
	r3[7] *= s;
	
	m2 = r2[3];			/* now back substitute row 2 */
	s = 1.f / r2[2];
	r2[4] = s * (r2[4] - r3[4] * m2), r2[5] = s * (r2[5] - r3[5] * m2),
	r2[6] = s * (r2[6] - r3[6] * m2), r2[7] = s * (r2[7] - r3[7] * m2);
	m1 = r1[3];
	r1[4] -= r3[4] * m1, r1[5] -= r3[5] * m1,
	r1[6] -= r3[6] * m1, r1[7] -= r3[7] * m1;
	m0 = r0[3];
	r0[4] -= r3[4] * m0, r0[5] -= r3[5] * m0,
	r0[6] -= r3[6] * m0, r0[7] -= r3[7] * m0;
	
	m1 = r1[2];			/* now back substitute row 1 */
	s = 1.f / r1[1];
	r1[4] = s * (r1[4] - r2[4] * m1), r1[5] = s * (r1[5] - r2[5] * m1),
	r1[6] = s * (r1[6] - r2[6] * m1), r1[7] = s * (r1[7] - r2[7] * m1);
	m0 = r0[2];
	r0[4] -= r2[4] * m0, r0[5] -= r2[5] * m0,
	r0[6] -= r2[6] * m0, r0[7] -= r2[7] * m0;
	
	m0 = r0[1];			/* now back substitute row 0 */
	s = 1.f / r0[0];
	r0[4] = s * (r0[4] - r1[4] * m0), r0[5] = s * (r0[5] - r1[5] * m0),
	r0[6] = s * (r0[6] - r1[6] * m0), r0[7] = s * (r0[7] - r1[7] * m0);

	invertetMatrix._11 = r0[4];
	invertetMatrix._12 = r0[5];
	invertetMatrix._13 = r0[6];
	invertetMatrix._14 = r0[7];
	invertetMatrix._21 = r1[4];
	invertetMatrix._22 = r1[5];
	invertetMatrix._23 = r1[6];
	invertetMatrix._24 = r1[7];
	invertetMatrix._31 = r2[4];
	invertetMatrix._32 = r2[5];
	invertetMatrix._33 = r2[6];
	invertetMatrix._34 = r2[7];
	invertetMatrix._41 = r3[4];
	invertetMatrix._42 = r3[5];
	invertetMatrix._43 = r3[6];
	invertetMatrix._44 = r3[7];
	
	return invertetMatrix;
	
	#undef SWAP_ROWS
}